package weaver.interfaces.cyjt.duss.action;

import com.weaver.general.BaseBean;
import weaver.common.DateUtil;
import weaver.conn.RecordSet;
import weaver.general.Util;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;

import java.math.BigDecimal;

/**
 * @Title: ecology-idea
 * @Company: 泛微软件
 * @author: weaver.Dusongsong
 * @version: 1.0
 * @CreateDate: 2024-12-11
 * @Description: 09.付款结算单formtable_main_77、10.付款结算单（银行托收及自动扣款）formtable_main_78、
 * 11.付款结算单（融资本金、利息）formtable_main_79、01.事项审批表（生成NC单据）
 * formtable_main_69明细表1里面会有一个本次付款金额和已付款金额，
 * 在出纳节点会填写本次付款金额，提交时，将本次付款金额加入到已付款金额内，
 * 并在明细表2留下相应的记录
 * @ModifyLog:
 **/
public class WeaAddDtl2GeneralFklcAction extends BaseBean implements Action {
    @Override
    public String execute(RequestInfo requestInfo) {
        String requestid = requestInfo.getRequestid();
        String tablename = requestInfo.getRequestManager().getBillTableName();
        writeLog(">>>>>WeaAddDtl2GeneralFklcAction>>>>>requestid=" + requestid);
        try {
            RecordSet rs = new RecordSet();
            RecordSet rs1 = new RecordSet();
            String currentDate = DateUtil.getCurrentDate();
            rs.executeQuery("select id from " + tablename + " where requestid=?", requestid);
            rs.next();
            String mainid = Util.null2String(rs.getString("id"));
            rs.executeQuery("select id,fysm,bcfkje,yfje from "+tablename+"_dt1 where mainid=?",mainid);
            while (rs.next()){
                String id = Util.null2String(rs.getString("id"));
                String fysm = Util.null2String(rs.getString("fysm"));
                String bcfkje = Util.null2String(rs.getString("bcfkje"));
                String yfje = Util.null2String(rs.getString("yfje"));
                String sql = "insert into "+tablename+"_dt2(mainid,zy,fkje,tjrq) values(?,?,?,?)";
                rs1.executeUpdate(sql,mainid,fysm,bcfkje,currentDate);
                String newYfje = bAdd(yfje,bcfkje);
                rs1.executeUpdate("update "+tablename+"_dt1 set yfje=? where mainid=? and id=?",newYfje,mainid,id);
            }
            return Action.SUCCESS;
        } catch (Exception e) {
            writeLog(">>>>>WeaAddDtl2GeneralFklcAction>>>>>Exception=" + e);
            requestInfo.getRequestManager().setMessageid("111111");
            requestInfo.getRequestManager().setMessagecontent("Action接口处理异常,请联系管理员");
            return Action.FAILURE_AND_CONTINUE;
        }
    }
    /**
     * @Description: 相加
     */
    public static String bAdd(String a, String b) {
        a = isZero(a);
        b = isZero(b);
        String result;
        try {
            BigDecimal bg1 = new BigDecimal(a);
            BigDecimal bg2 = new BigDecimal(b);
            BigDecimal bd = bg1.add(bg2);
            result = bd.toString();
        } catch (NumberFormatException e) {
            result = "";
        }
        return result;
    }
    /**
     * @Description: 为零判断
     */
    public static String isZero(String a) {
        if ("".equals(a)) {
            a = "0";
        }
        return a;
    }

}